$(function () {
    fillNewsTypeOption();
    getNewsSortListByPage();
});
var total = 0;

//为当前显示条数文本框绑定回车事件
$('#size').bind('keypress', function (event) {
    if (event.keyCode == "13") {
        //敲击回车后重新按照新的显示条数加载页面
        getNewsSortListByPage();
    }
});

//为当前页数文本框绑定回车事件
$('#pageipt').bind('keypress', function (event) {
    if (event.keyCode == "13") {
        getNewsSortListByPage();
    }
});

//为上一页绑定点击事件
$('#lastpage').click(function () {
    //获取默认的当前页数和显示条数值
    let page = $("#pageipt").val();
    page--;
    if (page >= 1) {//如果不是第一页，则往前翻一页
        $("#pageipt").val(page);
        getNewsSortListByPage();
    }
});

//绑定下一页点击事件
$('#nextpage').click(function () {
    //获取默认的当前页数和显示条数值
    let page = $("#pageipt").val();
    let size = $("#size").val();
    page++;
    if (page <= Math.ceil(total / size)) {
        $("#pageipt").val(page);
        getNewsSortListByPage();
    }
});

//为首页绑定点击事件
$("#first").click(function () {
    let page = $("#pageipt").val();
    if (page > 1) {//如果当前页不是首页则跳转到首页，否则不做任何事情
        page = 1;
        $("#pageipt").val(page);
        getNewsSortListByPage();
    }
});

//为尾页绑定点击事件
$("#last").click(function () {
    let page = $("#pageipt").val();
    let size = $("#size").val();
    if (page < Math.ceil(total / size)) {//如果当前页不是尾页，则跳转到尾页
        page = Math.ceil(total / size);
        $("#pageipt").val(page);
        getNewsSortListByPage();
    }
});

function getNewsList() {
    let data = myAjax('/back/news', 'get', '', 'json');
    console.log(data.news);
    $("tbody").html("");
    $.each(data.news, function (i, news) {
        $("tbody").append("<td><input type='checkbox'></td><td>" + news.id + "</td><td>" + news.title + "</td><td>" + news.newsType.name + "</td><td>" + news.createTime + "</td><td>" + news.content.substring(0, 15) + "...</td><td><button onclick='directEditNews(" + news.id + ")'>编辑</button><button onclick='deleteNews(" + news.id + ")'>删除</button></td>");
    });
}

function getNewsListByPage() {
    //获取默认的当前页数和显示条数值
    let page = $("#pageipt").val();
    let size = $("#size").val();

    //判断显示条数输入框中的值如果为空或者是非正数，则将size默认赋值为10，页面显示条数默认显示10条
    if (size.trim() == '' || size.trim() <= 0) {
        size = 5;
    }

    //判断页数输入框中的值如果为空或者是非正数，则将page默认赋值为1，页面默认显示第一页
    if (page.trim() == '' || page.trim() <= 0) {
        page = 1;
    }
    console.log(page + "," + size);
    let data = {
        "page": page,
        "size": size
    };
    let result = myAjax('/back/news', 'post', data, 'json');
    console.log(result.news);
    $("tbody").html("");
    $.each(result.news.list, function (i, news) {
        $("tbody").append("<td><input type='checkbox'></td><td>" + news.id + "</td><td>" + news.title + "</td><td>" + news.newsType.name + "</td><td>" + news.createTime + "</td><td>" + news.content.substring(0, 15) + "...</td><td><button class='iconfont' onclick='directEditNews(" + news.id + ")'>&#xe608;</button><button class='iconfont' onclick='deleteNews(" + news.id + ")'>&#xe618;</button></td>");
    });
    total = result.news.total;
    //显示总条数
    $("#pagecount").text(total);
    console.log(total);
}

function getNewsSortListByPage() {
    //获取默认的当前页数和显示条数值
    let title = $.trim($("#title").val());
    let author = $.trim($("#author").val());
    let news_type = $("#news_type").find("option:selected").val();
    let start_date = $("#start_date").val();
    let end_date = $("#end_date").val();
    let page = $("#pageipt").val();
    let size = $("#size").val();
    let sort = $("#sort").find("option:selected").val();
    //判断显示条数输入框中的值如果为空或者是非正数，则将size默认赋值为10，页面显示条数默认显示10条
    if (size.trim() == '' || size.trim() <= 0) {
        size = 5;
    }

    //判断页数输入框中的值如果为空或者是非正数，则将page默认赋值为1，页面默认显示第一页
    if (page.trim() == '' || page.trim() <= 0) {
        page = 1;
    }
    console.log(page + "," + size + "," + sort + "," + news_type);
    let data = {
        'title': title,
        'author':author,
        'news_type': news_type,
        'start_date': start_date,
        'end_date': end_date,
        'page': page,
        'size': size,
        'sort': sort
    };
    let result = myAjax('/back/news/info', 'post', data, 'json');
    $("tbody").html("");
    console.log(result);
    $.each(result.news.list, function (i, news) {
        $("tbody").append("<tr><td><input type='checkbox'></td><td>" + news.id + "</td>" +
            "<td>" + news.title.substring(0,10) + "...</td><td>" + news.author + "</td>" +
            "<td>" + news.newsType.name + "</td><td>" + news.create_time + "</td>" +
            "<td>" + news.intro.substring(0, 10) + "...</td>" + setShow(news.id, news.isShow) +
            "<td><button class='iconfont' onclick='directEditNews(" + news.id + ")'>&#xe608;</button>" +
            "<button class='iconfont' onclick='deleteNews(" + news.id + ")'>&#xe618;</button></td></tr>");
    });
    total = result.news.total;
    //显示总条数
    $("#pagecount").text(total);
    console.log(total);
}

function setShow(id, show) {
    if (show == 1)
        return "<td><i class='iconfont' onclick='updateShow(" + id + ",0)'>&#xe60a;</i></td>";
    else
        return "<td><i class='iconfont' onclick='updateShow(" + id + ",1)'>&#xe610;</i></td>";
}

function updateShow(id, show) {
    let data = myAjax("/back/news/show", 'post', {"id": id, "show": show}, 'text');
    if (data == 1)
        getNewsSortListByPage();
    else
        alert("异常！");
}

function directEditNews(id) {
    sessionStorage.setItem("newsid", id);
    $(".right").load("/html/back/news/editnews.html");
}

function deleteNews(id) {
    let flag = confirm("确定删除？");
    if (flag) {
        let data = myAjax("/back/news/manage", 'get', {"id": id}, 'text');
        if (data > 0)
            alert("删除成功！");
        else
            alert("删除异常！");
        getNewsSortListByPage();
    }
}

//搜索用户
$('#find').click(function () {
    getNewsSortListByPage()
});

function fillNewsTypeOption() {
    let data = myAjax('/back/news/type', 'get', '', 'json');
    console.log(data);
    $("#news_type").html("");
    $("#news_type").append("<option value='全部'>全部</option>");
    $.each(data.newstype, function (i, newstype) {
        $("#news_type").append("<option value='" + newstype.id + "'>" + newstype.name + "</option>");
    });
}